package com.znjc.web.controller.device;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.znjc.common.core.controller.BaseController;
import com.znjc.common.core.domain.AjaxResult;
import com.znjc.common.utils.StringUtils;
import com.znjc.device.domain.DeviceBoard;
import com.znjc.device.domain.DeviceBoardRef;
import com.znjc.device.domain.DeviceDevice;
import com.znjc.device.domain.vo.DeviceBoardVO;
import com.znjc.device.service.IDeviceBoardService;
import com.znjc.device.service.IDeviceDeviceService;
import com.znjc.device.util.DataUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 【通道转换板】Controller
 *
 * @author zengpc
 * @date 2024-01-22
 */
@RestController
@RequestMapping("/device/board")
public class DeviceBoardController extends BaseController {
    @Autowired
    private IDeviceBoardService deviceBoardService;
    @Autowired
    private IDeviceDeviceService deviceDeviceService;
    /**
     * 通道转换板
     */
    @PreAuthorize("@ss.hasPermi('device:board:edit')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        List<DeviceBoard> list = deviceBoardService.list();
        List<DeviceBoardVO> vos = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(list)) {
            //封装所有的板子号，通道号
            list.forEach(board -> {
                DeviceBoardVO vo = new DeviceBoardVO();
                vo.setId(board.getId());
                vo.setBoardCode(Integer.parseInt(board.getBoardCode()));
                vo.setInterestList(DataUtil.getPassageVOs());
                vos.add(vo);
            });
            //已选
            DeviceDevice device = deviceDeviceService.getById(id);
            if (null != device) {
                //主测点
                if(StringUtils.isNotEmpty(device.getMasterCode())){
                    for (DeviceBoardVO vo : vos) {
                        if (Integer.parseInt(device.getMasterCode().substring(0,1)) == (vo.getBoardCode())) {
                            vo.getIscheckAll().add(device.getMasterCode().substring(1));
                            vo.getInterestListArry().add(device.getMasterCode());
                            break;
                        }
                    }
                }
                //从测点
                if(StringUtils.isNotEmpty(device.getSlaveCode())){
                    String[] slaveBits = device.getSlaveCode().split(",");
                    if(slaveBits.length > 0){
                        for(String slaveCode : slaveBits){
                            for (DeviceBoardVO vo : vos) {
                                if (Integer.parseInt(slaveCode.substring(0,1)) == (vo.getBoardCode())) {
                                    vo.getIscheckSlave().add(slaveCode.substring(1));
                                    vo.getInterestListSlaveArry().add(slaveCode);
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
        return success(vos);
    }

}
